package com.example.lab.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.lab.entity.Semesters;
import com.example.lab.entity.dto.QueryDto;
import com.example.lab.mapper.SemestersMapper;
import com.example.lab.service.ISemestersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 学期信息表 服务实现类
 * </p>
 *
 * @author 布莱恩
 * @since 2025-06-17
 */
@Service
public class SemestersServiceImpl extends ServiceImpl<SemestersMapper, Semesters> implements ISemestersService {

    @Override
    public List<Semesters> getAllSemesters() {
        QueryWrapper<Semesters> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("start_date");
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public Page<Semesters> getSemestersByPage(QueryDto queryDto) {
        Page<Semesters> page = new Page<>(queryDto.getPage(), queryDto.getLimit());
        QueryWrapper<Semesters> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("start_date");
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public Semesters createSemester(Semesters semester) {
        semester.setCreatedAt(LocalDateTime.now());
        baseMapper.insert(semester);
        return semester;
    }

    @Override
    public boolean updateSemester(Semesters semester) {
        return baseMapper.updateById(semester) > 0;
    }

    @Override
    public boolean deleteSemester(Integer semesterId) {
        return baseMapper.deleteById(semesterId) > 0;
    }

    @Override
    public Semesters getSemesterById(Integer semesterId) {
        return baseMapper.selectById(semesterId);
    }
}
